package sel.me.spring.boot159.web.config;

import com.alibaba.druid.support.http.StatViewServlet;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author shenenlu 2021年01月18日 下午12:15:51
 */
@Configuration
public class DruidServletConfig {

    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        registrationBean.addInitParameter("allow", "127.0.0.1");// IP白名单 (没有配置或者为空，则允许所有访问)
        registrationBean.addInitParameter("deny", "");// IP黑名单 (存在共同时，deny优先于allow)
//        registrationBean.addInitParameter("loginUsername", "root");
//        registrationBean.addInitParameter("loginPassword", "1234");
        registrationBean.addInitParameter("resetEnable", "false");
        return registrationBean;
    }

//    @Bean
//    public ServletRegistrationBean registerAuthFilter(StatViewServlet statViewServlet) {
//        ServletRegistrationBean registration = new ServletRegistrationBean();
//        registration.setServlet(statViewServlet);
//        Set<String> urls = new HashSet<>();
//        urls.add("/druid/*");
//        registration.setUrlMappings(urls);
//        registration.setName("DruidWebStatServlet");
//        registration.setOrder(1);  //值越小，Filter越靠前。
//        return registration;
//    }
//
//    @Bean
//    public StatViewServlet druidWebStatServlet() {
//        StatViewServlet statViewServlet = new StatViewServlet();
//        return statViewServlet;
//    }

}
